<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-container__search">
        <el-input
          v-model="listQuery.keyword"
          clearable
          placeholder="请输入手机号或模板名称"
          @keyup.enter.native="onFilter"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            type="primary"
            @click="onFilter"
            @keyup.enter.native="onFilter"
            >搜索</el-button
          >
        </el-input>
      </div>
      <div class="filter-container__ctrl">
        <!-- <el-button
          class="filter-item"
          style="margin-left: 10px"
          type="primary"
          plain
          icon="el-icon-edit"
          @click="handleCreate"
        >
          {{ $t('table.add') }}
        </el-button> -->
      </div>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      stripe
      @selection-change="handleSelectionChange"
      id="tableList"
    >
      <el-table-column label="手机号">
        <template slot-scope="{ row }"> {{ row.phone }} </template>
      </el-table-column>

      <el-table-column label="来源模板ID">
        <template slot-scope="{ row }"> {{ row.pageId }} </template>
      </el-table-column>

      <el-table-column label="来源模板名称">
        <template slot-scope="{ row }"> {{ row.name }} </template>
      </el-table-column>

      <el-table-column label="用户信息">
        <template slot-scope="{ row }"> {{ row.user }} </template>
      </el-table-column>

      <el-table-column
        sortable
        prop="updatedAt"
        label="提交时间"
        align="center"
      >
        <template slot-scope="{ row }">
          <span>{{ row.updatedAt }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchList, remove } from '@/api/form'

import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'FormList',
  components: {
    Pagination,
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id',
        keyword: '',
        pageId: '',
      },
      downloadLoading: false,
      selectedRows: [],
    }
  },
  computed: {
    userinfo: function () {
      return this.$store.state.user.info
    },
  },
  watch: {
    'listQuery.keyword'(value) {
      if (value.length <= 0) {
        this.getList()
      }
    },
  },
  async created() {
    const { isAdmin, pages = [] } = this.userinfo
    const ids = isAdmin !== 1 && pages.length > 0 ? pages : ''
    this.listQuery.pageId = ids

    await this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then((res) => {
        const { data = [], total = 0 } = res
        this.list = data
        this.total = total
        this.listLoading = false
      })
    },

    // 删除
    handleDelete(row) {
      let ids = []
      if (Array.isArray(row)) {
        ids = row.map((v) => v.id)
      } else {
        ids.push(row.id)
      }

      this.handleClose(() => {
        remove({
          ids,
        }).then(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000,
          })
          this.getList()
        })
      })
    },

    handleSelectionChange(val) {
      this.selectedRows = val
    },

    // 删除提醒
    handleClose(done) {
      this.$confirm('确认删除？')
        .then(() => {
          done()
        })
        .catch(() => {})
    },

    // filter
    onFilter() {
      this.listQuery.page = 1
      this.getList()
    },
  },
}
</script>
